﻿from selenium import webdriver  # 用来驱动浏览器的
from selenium.webdriver import ActionChains  # 破解滑动验证码的时候用的 可以拖动图片
from selenium.webdriver.common.by import By  # 按照什么方式查找，By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys  # 键盘按键操作
from selenium.webdriver.support import expected_conditions as EC  # 和下面WebDriverWait一起用的
from selenium.webdriver.support.wait import WebDriverWait  # 等待页面加载某些元素
import pycurl
#python main.py --url https://www.pixwox.com/zh-hans/profile/remu19971203/ --path g:/download3 --times=30
#python main.py --url https://www.pixwox.com/zh-hans/profile/darkasqa/ --path g:/download3 --times=30
#python main.py --url https://www.pixwox.com/zh-hans/profile/asukakiraran/ --path g:/download3 --times=30
#python main.py --url https://www.pixwox.com/zh-hans/profile/epoint2016/ --path g:/download3 --times=30
import time
import argparse
import os
import wget
import certifi
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--url',type=str,nargs='+')
    parser.add_argument('--path',type=str,nargs='+')
    parser.add_argument('--times',type=int,nargs='+')
    args = parser.parse_args()
    print(args)
    targeturl =args.url[0]
    targetpath =args.path[0]
    times =args.times[0]
    options = webdriver.ChromeOptions()

    prefs = {'profile.default_content_settings.popups': 0,
         'download.default_directory': targetpath}
    options.add_experimental_option('prefs', prefs)
    options.add_argument('--disable-gpu')
    options.add_argument('--ignore-certificate-errors')
    browser = webdriver.Chrome(chrome_options=options)
    browser.implicitly_wait(6) 

    browser.get(targeturl)
    time.sleep(3)
# 下拉滚动条, 循环多次下拉, 可以获取到更多的图片
    for i in range(times):
        browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
        time.sleep(1)
        more_btns=browser.find_elements_by_class_name('more_btn')
        if more_btns:
            for b in more_btns :
                try:
                    b.click()
                except Exception as e:
                    print("error:"+str(e))
    links=browser.find_elements_by_class_name('cover_link')
    urls=[]
    for link in links :
        href=link.get_attribute('href')
        print(link.get_attribute('href'))
        urls.append(href)




    for url in urls:

        imgurls =[]
        browser.implicitly_wait(6) 
        try:
            browser.get(url)
            imgs=browser.find_elements_by_xpath("//div[@class='pic']//img")
            for img in imgs :
                imgurl=img.get_attribute('src')
                if imgurl:
                   imgurls.append(imgurl)
                   print(imgurl)
            for imgurl in imgurls:
                #browser.implicitly_wait(6) 
                #browser.get(imgurl)
                #file_name =wget.filename_from_url(imgurl)
                #browser.find_element_by_xpath("//body/img[1]").screenshot(os.path.join(targetpath, file_name))
                imgurl=imgurl[0:imgurl.index("?")]
                print(imgurl)
                file_name =wget.filename_from_url(imgurl)
                
                #wget.download(url, out=os.path.join(targetpath, file_name))
                '''
                c = pycurl.Curl() #创建一个Curl对象
    
                c.setopt(pycurl.URL, imgurl)               #定义请求的URL常量
                c.setopt(pycurl.CONNECTTIMEOUT, 50)      #定义请求连接的等待时间
                c.setopt(pycurl.TIMEOUT, 50)             #定义请求超时时间
                imgfile = open(os.path.join(targetpath, file_name), "wb")
                c.setopt(pycurl.WRITEDATA, imgfile) 
                c.setopt(pycurl.SSL_VERIFYPEER, 0)
                c.setopt(pycurl.SSL_VERIFYHOST, 0)
                try:
    
                    c.perform() #提交请求
                    # HTTP response code, e.g. 200.
                    print('Status: %d' % c.getinfo(c.RESPONSE_CODE))
                    # Elapsed time for the transfer.
                    print('Time: %f' % c.getinfo(c.TOTAL_TIME))
                except Exception as e:
                    print("connecion error:"+str(e))
                    imgfile.close()
                    c.close()
                '''
                returned_value  =os.system("curl -k -o "+os.path.join(targetpath, file_name)+" "+imgurl) 
                print('returned value:', returned_value)   
        except Exception as e:
            print("error:"+str(e))
    browser.quit()